package com.egoint.controller;

import com.egoint.model.Member;
import com.egoint.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/8/25 0025.
 */
@Controller
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/member/info")
    public ModelAndView memberInfo(@RequestParam(value="id", required=true, defaultValue="0") String id, Model model) {
        System.out.println(id);
        Member member = memberService.getMemberInfo(Integer.parseInt(id));
        model.addAttribute("member",member);
        ModelAndView mv = new ModelAndView("member");
        mv.addObject("member",member);
        mv.addObject("title","TEST TITLE");
        return mv;
    }

    @RequestMapping("/member/list")
    @ResponseBody
    public String members() {
        String body = "";
        ArrayList<Member> members = memberService.getMembers();
        System.out.println(members.size());

        if(members != null) {
            for (Member m :
                    members) {
                body += m.getUserName();
            }
        }
        return body;
    }
}
